package com.wankuma.hcm.midlet.metwit;

import METwit;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

public class ConfigForm extends Form implements CommandListener {
	private Command cmdApply = new Command("適用", Command.OK, 1);
	private Command cmdCancel = new Command("取消", Command.CANCEL, 2);

	TextField txtUser = new TextField("ﾕｰｻﾞ名", "", 64, TextField.ANY);
	TextField txtPassword = new TextField("ﾊﾟｽﾜｰﾄﾞ", "", 64, TextField.PASSWORD);
	TextField txtPageCapcity = new TextField("ﾍﾟｰｼﾞ件数", "", 3,
			TextField.NUMERIC);
	TextField txtTimeLineCapacity = new TextField("保持件数", "", 3,
			TextField.NUMERIC);
	TextField txtTimerInterval = new TextField("自動取得間隔(分)", "", 2,
			TextField.NUMERIC);

	public ConfigForm() {
		super("環境設定");

		this.addCommand(cmdApply);
		this.addCommand(cmdCancel);
		this.setCommandListener(this);

		Config config = Config.getInstance();
		txtUser.setString(config.getUser());
		txtUser.setInitialInputMode("UCB_BASIC_LATIN");
		txtPassword.setString(config.getPassword());
		String tmp = Integer.toString(config.getPageCapacity());
		txtPageCapcity.setString(tmp);
		tmp = Integer.toString(config.getTimeLineCapacity());
		txtTimeLineCapacity.setString(tmp);
		tmp = Integer.toString(config.getTimerInterval());
		txtTimerInterval.setString(tmp);
		this.append(txtUser);
		this.append(txtPassword);
		this.append(txtPageCapcity);
		this.append(txtTimeLineCapacity);
		this.append(txtTimerInterval);
		this.append("注：実際には数秒長い間隔になります。");
	}

	public void commandAction(Command c, Displayable d) {
		if (c == cmdApply) {
			Config config = Config.getInstance();
			config.setUser(txtUser.getString());
			config.setPassword(txtPassword.getString());
			config.setPageCapacity(Integer.parseInt(txtPageCapcity.getString()));
			config.setTimeLineCapacity(Integer.parseInt(txtTimeLineCapacity.getString()));
			config.setTimerInterval(Integer.parseInt(txtTimerInterval.getString()));
			config.saveConfig();
			METwit.returnToMainScreen();
		} else if (c == cmdCancel) {
			// ｷｬﾝｾﾙ
			METwit.returnToMainScreen();
		}
	}
}
